home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Libraries
/
SAT 2.3b4
/
Demo ƒ
/
HeartQuest demo ƒ
/
Preferences.p
< prev
next >
Wrap
Text File
|
1995-01-16
|
7KB
|
238 lines
{Preferences file handling}
{Principle: If we can write in the app itself, we do, but if not, or if there is already}
{a prefs file, we use a pref file in the system folder. (The idea is that it is very elegant}
{to save prefs in the application, whenever that is allowable.)}
{New version (march -94), stand-alone, takes file name and type as parameters,}
{can be forced to create a pref file, includes resource copying routine.}
{Revised 12/10-94: Cleaned up some junk.}
{What can we improve? Removing the last globals?}
{Kolla "shared"-flagga och sätta alwaysExternal efter den? (Bra idé!) Kan man kolla om}
{file sharing är på?}
unit Preferences;
{Made with help from the sources of}
{DeHQX v2.0.0 © Peter Lewis, Aug 1991 }
{Thanks, Peter, for sharing your sources with us!}
interface
{$ifc UNDEFINED THINK_PASCAL}
uses
Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps, Desk, Memory, SegLoad, Scrap, ToolUtils, OSEvents, OSUtils, Menus, Resources, StandardFile, GestaltEqu, Files, Errors;
{$endc}
var
gAppFile, gPrefFile: integer; {refnumbers to pass to UseResFile.}
sysenv: SysEnvRec;
function SetPrefFile (prefsFileName: Str255; prefCreator, prefType: OSType; alwaysExternal: Boolean): Boolean;
{Open the pref file if needed. Set the global variables gAppFile and gPrefFile.}
{If alwaysExternal is true, we always want a pref file in the system folder even if we can save in the application.}
{Returns true if a new gPrefFile was created.}
{Copy a resource from one file to another. Useful when SetPrefFile returns true!}
function CopyResource (fromFile, toFile: integer; theResType: ResType; id: integer): OSErr;
implementation
{ 1) hitta Preferencefolder}
{ 2) Kolla om prefsfil existerar. I så fall, öppna den och gå till 6. }
{ 3) Hämta resurs och ändra den med ChangeResource. Om det gick, gå till 6.}
{ 4) Skapa prefsfil och öppna den.}
{ 5) Hämta resurser ur programmet och kopiera in i prefsfilen}
{ 6) Hämta resurser och skapa om de inte finns.}
const
{ From Folders:}
kPreferencesFolderType = 'pref'; {preferences for applications go here}
{ From Folders:}
function FindFolder (vRefNum: INTEGER; folderType: OSType; createFolder: BOOLEAN; var foundVRefNum: INTEGER; var foundDirID: LONGINT): OSErr;
inline
$7000, $A823;
function GetDirID (wdrn: integer; var vrn: integer; var dirID: longInt): OSErr;
var
procID: longInt;
oe: OSErr;
begin
oe := GetWDInfo(wdrn, vrn, dirID, procID);
if oe <> noErr then
begin
vrn := wdrn;
dirID := 0;
end;
GetDirID := oe;
end;
function GetPrefsFolder (var ovrn: integer): OSErr;
var
vrn: integer;
sDirID: longint;
oe: OSErr;
pb: WDPBRec;
oDirID: longint; {förr var-deklarerad utvariabel}
oVolID: integer;{förr var-deklarerad utvariabel}
begin
if SysEnvirons(1, sysenv) <> noErr then
;
vrn := sysenv.sysVRefNum;
if sysenv.systemVersion >= $0700 then {System7}
begin
sDirID := 0;
oe := GetDirID(vrn, vrn, sDirID);
oe := FindFolder(vrn, kPreferencesFolderType, true, oVolID, oDirID);
{Convert FindFolders volref+dirid to WDref}
pb.ioVRefNum := oVolID;
pb.ioWDProcID := longint('ERIK');
pb.ioWDDirID := oDirID;
pb.ioWDIndex := 0;
{pb.ioWDVRefNum := -1; {???}
pb.ioNamePtr := nil;
pb.ioCompletion := nil;
oe := PBOpenWD(@pb, false);
ovrn := pb.ioVRefNum; {WDRefNum}
end
else
begin
ovrn := vrn;
oe := NoErr;
end;
GetPrefsFolder := oe;
end; {GetPrefsFolder}
{function SetPrefFile: boolean;}
function SetPrefFile (prefsFileName: Str255; prefCreator, prefType: OSType; alwaysExternal: Boolean): Boolean;
var
err: OSErr;
prefsFolder: integer;
h: handle;
s: str255;
begin
gAppFile := CurResFile; {spara programmerts resursfilreferens}
gPrefFile := 0;
SetPrefFile := false;
{ 1) hitta Preferencefolder}
err := GetPrefsFolder(prefsFolder);{, PrefsFolderId, PrefsVolId}
if err <> NoErr then
;
{SATReportStr('Error finding system folder.')}
{ 2) Kolla om prefsfil existerar. I så fall, öppna den och gå till 6. }
gPrefFile := OpenRFPerm(prefsFileName, prefsFolder, FSRdWrPerm);
if ResError = noErr then
begin
end
else
begin
gPrefFile := 0;
{ 3) Get some resource and change it with ChangeResource. If that works, go to 6.}
{Om alwaysExternal är true behöver vi inte kolla - då vill vi ha en }
if not alwaysExternal then
begin
h := Get1IndResource(prefCreator, 1); {The signatur always exists if it has a BNDL.}
if h = nil then
begin
{Error! Resursen saknas!}
{SATReportStr('Resource missing!');}
end;
ChangedResource(h);
alwaysExternal := ResError <> noErr; {Lite fult att ändra "always", men nu vill vi ju ha extern!}
ReleaseResource(h); {OK, done with the resource}
end;
if alwaysExternal then
begin
{ 4) Create prefs file and open it.}
err := Create(PrefsFileName, prefsFolder, prefCreator, prefType);
if err = noErr then
begin
;
{SATReportStr('Application locked. Creating prefs file instead.')}
HCreateResFile(prefsFolder, 0, prefsFileName);{???}
if ResError <> noErr then
begin
{SATReportStr('Couldn''t create resource fork!');}
end
else
gPrefFile := OpenRFPerm(prefsFileName, prefsFolder, FSRdWrPerm);
if ResError = noErr then
SetPrefFile := true; {new pref file!}
{ 5) Hämta resurser ur programmet och kopiera in i prefsfilen}
end{Create succeeded}
else
{SATReportStr('Failed to create prefsfile!')}
;
end; {Programmet skrivskyddat.}
end; {prefsfil existerade inte}
{ 6) Hämta resurser och skapa om de inte finns.}
end;
{Kopiera resurs:}
function CopyResource (fromFile, toFile: integer; theResType: ResType; id: integer): OSErr;
var
oldResFile: integer;
res, rescopy: Handle;
wasLoaded: Boolean;
theID: integer;
theType: ResType;
theName: Str255;
procedure Barf;
begin
CopyResource := ResError;
UseResFile(oldResFile); {återställ}
exit(CopyResource);
end;
procedure CheckError;
begin
if ResError <> noErr then
Barf;
end; {CheckError}
begin
oldResFile := CurResFile;
UseResFile(fromFile);
CheckError;
SetResLoad(false);
res := Get1Resource(theResType, id);
{Don't CheckError before doing SetResLoad(true)!!!}
SetResLoad(true);
CheckError;
if res <> nil then
begin
wasLoaded := res^ = nil;
LoadResource(res);
CheckError;
UseResFile(toFile);
CheckError;
GetResInfo(res, theID, theType, theName);
CheckError;
rescopy := res;
if HandToHand(rescopy) <> noErr then
Barf; {i stället för DetachResource(res);}
CheckError;
AddResource(rescopy, theResType, id, theName);
CheckError;
ReleaseResource(rescopy);
if not wasLoaded then
ReleaseResource(res); {If it wasn't loaded before, it shouldn't be afterwards.}
CheckError;
CopyResource := noErr;
end
else
begin
CopyResource := resNotFound;
end;
UseResFile(oldResFile);
end;
end.